# import requests, bs4
# url = 'https://www.bilibili.com/v/popular/rank/all'
# html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}).text
# soup = bs4.BeautifulSoup(html, 'lxml')
# print('页面标题：', soup.title.string)

# import requests
# url = 'https://www.bilibili.com/v/popular/rank/all'
# html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}).text
# print(html[:200])      # 只看前 200 字符

# import requests, pandas as pd
#
# url = 'https://api.bilibili.com/x/web-interface/ranking?rid=0&type=origin'
# headers = {'User-Agent': 'Mozilla/5.0'}
# data = requests.get(url, headers=headers).json()
#
# # 提取前 5 条示范
# top5 = [
#     {'title': item['title'],
#      'play': item['stat']['view'],
#      'up': item['owner']['name']}
#     for item in data['data']['list'][:5]
# ]
#
# df = pd.DataFrame(top5)
# print(df)

# import requests
#
# url = 'https://api.bilibili.com/x/web-interface/ranking?rid=0&type=origin'
# data = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}).json()
#
# # 只看第一条的结构
# first = data['data']['list'][0]
# for key, value in first.items():
#     print(f'{key}: {type(value)}')

import requests, pandas as pd

url = 'https://api.bilibili.com/x/web-interface/ranking?rid=0&type=origin'
data = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}).json()

top5 = [{'title': item['title'],
         'play': item['play'],
         'up': item['author']}
        for item in data['data']['list'][:5]]

print(pd.DataFrame(top5))